package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.entity.ApartmentFacility;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 20154
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-10-30 17:26:47
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{
        @Autowired
        ApartmentInfoMapper apartmentInfoMapper;
        @Autowired
        ApartmentFacilityMapper apartmentFacilityMapper;
        @Autowired
    ApartmentLabelMapper apartmentLabelMapper;
        @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;
        @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    CityInfoMapper cityInfoMapper;
    @Autowired
    DistrictInfoMapper districtInfoMapper;

@Autowired
FacilityInfoMapper facilityInfoMapper;
@Autowired
LabelInfoMapper labelInfoMapper;


@Autowired
RoomInfoMapper roomInfoMapper;

@Autowired
    RedisTemplate redisTemplate;
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean existsId = apartmentSubmitVo.getId() != null;

        //根据地区id存储位置信息
        Long provinceId = apartmentSubmitVo.getProvinceId();
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(provinceId);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        Long cityId = apartmentSubmitVo.getCityId();
        CityInfo cityInfo = cityInfoMapper.selectById(cityId);
        apartmentSubmitVo.setCityName(cityInfo.getName());

        Long districtId = apartmentSubmitVo.getDistrictId();
        DistrictInfo districtInfo = districtInfoMapper.selectById(districtId);
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        super.saveOrUpdate(apartmentSubmitVo);
        if (existsId){
            //删除公寓配套列表
            apartmentFacilityMapper.delete(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId()));

            //删除公寓标签列表
            apartmentLabelMapper.delete(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId()));
            //删除公寓费用列表
            apartmentFeeValueMapper.delete(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId()));
            //删除图片列表
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            queryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoMapper.delete(queryWrapper);
        }

        // 插入图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfos.add(graphInfo);

                // 把把保存到数据库的图片信息，再保存到redis中
                String url = graphVo.getName();
                String fileName = url.substring(url.lastIndexOf("lease/")+6);
                String dbImageKey = "db_image:key";
                redisTemplate.opsForSet().add(dbImageKey,fileName);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        // 插入配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> list = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.
                        builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
//                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
//                apartmentFacility.setFacilityId(facilityInfoId);
                list.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(list);
        }
        // 插入标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            ArrayList<ApartmentLabel> apartmentLabelArrayList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.
                        builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
//                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
//                apartmentLabel.setLabelId(labelId);
                apartmentLabelArrayList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelArrayList);
        }
            // 插入费用
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
                for (Long feeValueId : feeValueIds) {
                    ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.
                            builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
//                    apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
//                    apartmentFeeValue.setFeeValueId(feeValueId);
                    apartmentFeeValues.add(apartmentFeeValue);
                }
                apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }




    }



    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(apartmentItemVoPage,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentInfo byId = this.getById(id);
        if (id == null){
            return null;
        }
        // 查询公寓图片
        List<GraphVo> graphVoList = graphInfoMapper.selectByApartmentId(ItemType.APARTMENT,id);
        // 查询公寓配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByApartmentId(id);
        // 查询公寓标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectByApartmentId(id);
        // 查询公寓费用
        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.selectByApartmentId(id);

        ApartmentDetailVo detailVo = new ApartmentDetailVo();

        BeanUtils.copyProperties(byId,detailVo);
        detailVo.setGraphVoList(graphVoList);
        detailVo.setFacilityInfoList(facilityInfoList);
        detailVo.setLabelInfoList(labelInfoList);
        detailVo.setFeeValueVoList(feeValueVoList);
        return detailVo;
    }

    @Override
    public void removeByApartmentId(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除公寓信息
        this.removeById(id);
        //删除公寓图集
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoService.remove(graphInfoQueryWrapper);
        //删除公寓配套
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        //删除公寓标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        //删除公寓费用
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
    }
}




